/****************************** (C) COPYRIGHT 2016 Rsestudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-20
* Description        : 
*****************************************************************************************/
/* Includes ----------------------------------------------------------------------------*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Bsp.h"
#include "device_pins.h"
#include "button.h"
#include "PowerManager.h"
#include "button.h"
#include "main.h"
#include "UI.h"
#include "UI_CondStart.h"
#include "oled.h" 	

#include "O2Sensor.h"
/* Extern variables --------------------------------------------------------------------*/
/* Private define ----------------------------------------------------------------------*/
/* Private macro -----------------------------------------------------------------------*/
/* Private typedef ---------------------------------------------------------------------*/
/* Private variables -------------------------------------------------------------------*/
static TimerHandle_t ui_timer = NULL;
static StaticTimer_t xui_timer;
static EVENT_SRCEEN event_screen;
static void cb_ui_timer(TimerHandle_t parameter);
static SemaphoreHandle_t mtx_process_event = NULL;     // 互斥量句柄
static StaticSemaphore_t xmtx_process_event;  // 存储互斥量的数据结构


static int32_t cond_finished;
/* Private functions -------------------------------------------------------------------*/
static HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID);
static HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID);

static HMI_SCREEN_ACTION	UI_action = 
{
	UI_Initialize,
	UI_Prepare,
	UI_Refresh,
	UI_ProcessEvent,
	UI_PostProcess,
	UI_ExitScreen,
};

HMI_SCREEN_OBJECT		UI_CondStart = 
{
	UI_SCREEN_ID_CondStart,
	&UI_action
};

/*****************************************************************************************
* Function Name  : UI_Welcome_Initialize
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev)
{	
	cond_finished = -10;
	mtx_process_event = xSemaphoreCreateMutexStatic(&xmtx_process_event);
	
	ui_timer = xTimerCreateStatic("tm_ui_period", pdMS_TO_TICKS(80), pdTRUE, NULL, cb_ui_timer, &xui_timer);
	
	HMI_EVENT_INIT(event_screen);
	
	return HMI_RET_NORMAL;
}

extern void width_set_point(int32_t width);
/*****************************************************************************************
* Function Name  : UI_ExitScreen
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev)
{	
	xTimerStop(ui_timer, portMAX_DELAY);
	
#if DEV_TYPE == DEV_TYPE_I7
//	OLED_Clear();
//	OLED_Refresh();
#elif DEV_TYPE == DEV_TYPE_XP
//	rses_led_control(&led_sys_r,0);
//	rses_led_control(&led_sys_g,0);
#elif DEV_TYPE == DEV_TYPE_MINI
//	rses_led_control(&led_sys_r,0);
//	rses_led_control(&led_sys_g,0);
#elif DEV_TYPE == DEV_TYPE_VACUUM

#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
    
#elif DEV_TYPE == DEV_TYPE_RPT_STD
    
    rt_thread_delay(800);
    
    if(iProfile.r_width.set_width == 0)
    {
        width_set_point(iProfile.r_width.set_width);
    }
    
#else
#error
#endif	
	vTaskDelay(pdMS_TO_TICKS(100));    /* 延时ms */
	return HMI_RET_NORMAL;
}


static uint8_t cond_time_instatus;
static uint8_t cond_routine;
static uint16_t led_timer;
static uint16_t instatus;
static uint8_t bat_timer;
static uint8_t end_timer;

/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters)
{			
	int start_mode;

	start_mode = (int)pParameters;
	    
	cond_finished = -10;
	
	iProfile.sys_status.CondStatus = 0;
	iProfile.sys_status.CondTimer = 0;	
	cond_time_instatus = 0;
	cond_routine = 0;
    bat_timer = 0;
	end_timer = 0;
    led_timer=0;
    instatus = 0;
    
    (void)start_mode;
    (void)end_timer;
    (void)instatus;
    (void)cond_time_instatus;
    (void)cond_routine;
    
    
    if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
		return HMI_RET_NORMAL;
	}

	if(iProfile.temperature.ColdTemper>=(iProfile.conf.start_max_temp*10))
	{
		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
		return HMI_RET_NORMAL;
	}
    		  
    meas_params_copy(&iProfile.meas_params,&iProfile.meas_params_cur);
    
#if DEV_TYPE == DEV_TYPE_I7 
    
	if(start_mode == COMMAND_CTRL_COND_START)
	{
		iProfile.cmd_ctrl.Status = COMMAND_CTRL_STS_COND;		
		if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_MANUAL)
		{
			HMI_SwitchScreen(UI_SCREEN_ID_Measure,(void*)MEAS_COND_START_COMAND);
			return HMI_RET_NORMAL;
		}
	}
	else if(start_mode == COMMAND_CTRL_DIRECT_START)
	{
		iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
		HMI_SwitchScreen(UI_SCREEN_ID_Measure,(void*)MEAS_COND_START_COMAND);
		return HMI_RET_NORMAL;
	}
	else
	{
		iProfile.cmd_ctrl.Status = COMMAND_CTRL_STS_COND;
		if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_MANUAL)
		{
			HMI_SwitchScreen(UI_SCREEN_ID_Measure,(void*)MEAS_COND_START_MANUAL);
			return HMI_RET_NORMAL;
		}
	}	
	
	xTimerStart(ui_timer, portMAX_DELAY);	
	led_timer=0;
	
#elif DEV_TYPE == DEV_TYPE_XP 

    rt_timer_start(&ui_timer);
	
#elif DEV_TYPE == DEV_TYPE_MINI
    
    rt_timer_start(&ui_timer);
    
#elif DEV_TYPE == DEV_TYPE_VACUUM
    	
	rt_timer_start(&ui_timer);	

#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

	rt_timer_start(&ui_timer);
    
#elif DEV_TYPE == DEV_TYPE_RPT_STD 
    
	if(start_mode == COMMAND_CTRL_COND_START)
	{
		iProfile.cmd_ctrl.Status = COMMAND_CTRL_STS_COND;		
		if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_MANUAL)
		{
			HMI_SwitchScreen(UI_SCREEN_ID_Measure,(void*)MEAS_COND_START_COMAND);
			return HMI_RET_NORMAL;
		}
	}
	else if(start_mode == COMMAND_CTRL_DIRECT_START)
	{
		iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
		HMI_SwitchScreen(UI_SCREEN_ID_Measure,(void*)MEAS_COND_START_COMAND);
		return HMI_RET_NORMAL;
	}
	else
	{
		iProfile.cmd_ctrl.Status = COMMAND_CTRL_STS_COND;
		if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_MANUAL)
		{
			HMI_SwitchScreen(UI_SCREEN_ID_Measure,(void*)MEAS_COND_START_MANUAL);
			return HMI_RET_NORMAL;
		}
	}	
	
	rt_timer_start(&ui_timer);	
	led_timer=0;	
#else
#error
#endif
	
	iProfile.cmd_ctrl.Status = COMMAND_CTRL_STS_COND;
	return HMI_RET_NORMAL;
}
//


void Cond_disp(void)
{
    static uint16_t cond_disp_timer;
    static uint16_t cond_disp_routine;
    static uint16_t cond_disp_instatus;
    
    (void)cond_disp_timer;
    (void)cond_disp_instatus;
    (void)cond_disp_routine;
    
    
    
#if DEV_TYPE == DEV_TYPE_I7
    
    lcd_fillarea(0,0,128,64,0);
    
    if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEMPERATURE)
    {
		OLED_Disp_Cond_Temperature(iProfile.meas_params_cur.Start_Params_1/10,iProfile.temperature.MaxTemperature/10);	      
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TIME)
    {	
		OLED_Disp_Cond_Time(iProfile.meas_params_cur.Start_Params_2/10,iProfile.sys_status.CondTimer/10);
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TorT)
    {		
		OLED_Disp_Cond_Time(iProfile.meas_params_cur.Start_Params_2/10,iProfile.sys_status.CondTimer/10);
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TandT)
    {
        if(cond_routine==0)
		{
			OLED_Disp_Cond_Time(iProfile.meas_params_cur.Start_Params_2/10,iProfile.sys_status.CondTimer/10);
        }
        else if(cond_routine==1)
		{	
			OLED_Disp_Cond_Temperature(iProfile.meas_params_cur.Start_Params_1/10,iProfile.temperature.MaxTemperature/10);
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEtoTI)
    {
        if(cond_routine==0)
		{
			OLED_Disp_Cond_Temperature(iProfile.meas_params_cur.Start_Params_1/10,iProfile.temperature.MaxTemperature/10);
        }
        else if(cond_routine==1)
		{	
			OLED_Disp_Cond_Time(iProfile.meas_params_cur.Start_Params_2/10,iProfile.sys_status.CondTimer/10);
        }
    }
    
    OLED_Refresh(); 
    
#elif DEV_TYPE == DEV_TYPE_XP

    if(WIFI_Connetstatusflag == 2)
    {
        rses_led_control(&led_ble,LED_COLOR_BLUE);
    }
    else
    {
        rses_led_control(&led_ble,LED_COLOR_BLACK);
    }
    
    if(iProfile.sys_status.VbusOnline)
    {
        if(iProfile.sys_status.BatChargeStatus == 1)
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }
    }
    else
    {
        if((iProfile.sys_status.BatLevelStatus < 10)&&(iProfile.sys_status.BatVoltage < 3400))
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }       
    }
    

    if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEMPERATURE)
    {			
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }      
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TIME)
    {	
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TorT)
    {		
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TandT)
    {
        if(cond_routine==0)
		{
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
        else if(cond_routine==1)
		{	
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEtoTI)
    {
        if(cond_routine==0)
		{
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
        else if(cond_routine==1)
		{	
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
    }
    
#elif DEV_TYPE == DEV_TYPE_MINI

    if(iProfile.Ble.Status == 2)
    {
        rses_led_control(&led_ble,LED_COLOR_BLUE);
    }
    else
    {
        rses_led_control(&led_ble,LED_COLOR_BLACK);
    }
    
    if(iProfile.sys_status.VbusOnline)
    {
        if(iProfile.sys_status.BatChargeStatus == 1)
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }
    }
    else
    {
        if((iProfile.sys_status.BatLevelStatus < 10)&&(iProfile.sys_status.BatVoltage < 3400))
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }       
    }
    

    if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEMPERATURE)
    {			
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }      
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TIME)
    {	
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TorT)
    {		
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TandT)
    {
        if(cond_routine==0)
		{
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
        else if(cond_routine==1)
		{	
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEtoTI)
    {
        if(cond_routine==0)
		{
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
        else if(cond_routine==1)
		{	
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
    }

#elif DEV_TYPE == DEV_TYPE_VACUUM
    
    if(iProfile.Ble.Status == 2)
    {
        rses_led_control(&led_ble,LED_COLOR_BLUE);
    }
    else
    {
        rses_led_control(&led_ble,LED_COLOR_BLACK);
    }
    
    if(iProfile.sys_status.VbusOnline)
    {
        if(iProfile.sys_status.BatChargeStatus == 1)
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }
    }
    else
    {
        if((iProfile.sys_status.BatLevelStatus < 10)&&(iProfile.sys_status.BatVoltage < 3400))
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }       
    }
    

    if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEMPERATURE)
    {			
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }      
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TIME)
    {	
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TorT)
    {		
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TandT)
    {
        if(cond_routine==0)
		{
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
        else if(cond_routine==1)
		{	
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEtoTI)
    {
        if(cond_routine==0)
		{
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
        else if(cond_routine==1)
		{	
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
    }
    
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
    
    if((iProfile.cmd_ctrl.b_meas_select & MEAS_SELECT_OXYGEN)!=0)
    {
        rses_led_control(&led_ble,LED_COLOR_BLUE);
    }
    else
    {
        rses_led_control(&led_ble,LED_COLOR_BLACK);
    }
    
    if(iProfile.sys_status.VbusOnline)
    {
        if(iProfile.sys_status.BatChargeStatus == 1)
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }
    }
    else
    {
        if((iProfile.sys_status.BatLevelStatus < 10)&&(iProfile.sys_status.BatVoltage < 3400))
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }       
    }
    

    if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEMPERATURE)
    {			
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }      
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TIME)
    {	
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TorT)
    {		
		if(cond_disp_timer++>3)
        {
            cond_disp_timer = 0;
            
            if(cond_disp_instatus == 0)
            {
                cond_disp_instatus = 1;
                rses_led_control(&led_run,LED_COLOR_RED);
            }
            else
            {
                cond_disp_instatus = 0;
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TandT)
    {
        if(cond_routine==0)
		{
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
        else if(cond_routine==1)
		{	
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEtoTI)
    {
        if(cond_routine==0)
		{
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
        else if(cond_routine==1)
		{	
			if(cond_disp_timer++>3)
            {
                cond_disp_timer = 0;
                
                if(cond_disp_instatus == 0)
                {
                    cond_disp_instatus = 1;
                    rses_led_control(&led_run,LED_COLOR_RED);
                }
                else
                {
                    cond_disp_instatus = 0;
                    rses_led_control(&led_run,LED_COLOR_BLACK);
                }
            }
        }
    }
    
    
#elif DEV_TYPE == DEV_TYPE_RPT_STD

    lcd_fillarea(0,0,128,64,0);

    if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEMPERATURE)
    {
		OLED_Disp_Cond_Temperature(iProfile.meas_params_cur.Start_Params_1/10,iProfile.temperature.MaxTemperature/10);	      
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TIME)
    {	
		OLED_Disp_Cond_Time(iProfile.meas_params_cur.Start_Params_2/10,iProfile.sys_status.CondTimer/10);
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TorT)
    {		
		OLED_Disp_Cond_Time(iProfile.meas_params_cur.Start_Params_2/10,iProfile.sys_status.CondTimer/10);
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TandT)
    {
        if(cond_routine==0)
		{
			OLED_Disp_Cond_Time(iProfile.meas_params_cur.Start_Params_2/10,iProfile.sys_status.CondTimer/10);
        }
        else if(cond_routine==1)
		{	
			OLED_Disp_Cond_Temperature(iProfile.meas_params_cur.Start_Params_1/10,iProfile.temperature.MaxTemperature/10);
        }
    }
    else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEtoTI)
    {
        if(cond_routine==0)
		{
			OLED_Disp_Cond_Temperature(iProfile.meas_params_cur.Start_Params_1/10,iProfile.temperature.MaxTemperature/10);
        }
        else if(cond_routine==1)
		{	
			OLED_Disp_Cond_Time(iProfile.meas_params_cur.Start_Params_2/10,iProfile.sys_status.CondTimer/10);
        }
    }
    
    OLED_Refresh(); 
    
#else 
#error
#endif    
}


/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters)
{
	//uint8_t temp_cond_timer;
	
	if(cond_finished == -1)
	{
		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
		return HMI_RET_NORMAL;
	}
	else  if(cond_finished >= 0)
	{
		HMI_SwitchScreen(UI_SCREEN_ID_Measure,(void*)cond_finished);
		return HMI_RET_NORMAL;
	}
	
	led_timer++;
	
	if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		bat_timer++;
	}else
	{
		bat_timer = 0;
	}
	
	if(bat_timer > 100)	//5s
	{
		cond_finished = -1;
		return HMI_RET_NORMAL;
	}
	
		
#if ((DEV_TYPE == DEV_TYPE_I7) ||(DEV_TYPE == DEV_TYPE_XP)||(DEV_TYPE == DEV_TYPE_MINI)||(DEV_TYPE == DEV_TYPE_VACUUM)||(DEV_TYPE == DEV_TYPE_RPT_STD) || (DEV_TYPE == DEV_TYPE_OXYGEN_STD))
	
    if(iProfile.cmd_ctrl.Meas_ctrl == COMMAND_CTRL_STOP)
	{
        oxy_sensor_stop();
		iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
		cond_finished = -1;
		return HMI_RET_NORMAL;
	}
    
	if(iProfile.cmd_ctrl.Meas_ctrl == COMMAND_CTRL_DIRECT_START)
	{
		iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
		cond_finished = MEAS_COND_START_COMAND;
		return HMI_RET_NORMAL;
	}
	
	if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_MANUAL)
	{
		cond_finished = MEAS_COND_START_MANUAL;
		return HMI_RET_NORMAL;
	}
	else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEMPERATURE)
	{			
		iProfile.cmd_ctrl.cond_stage = MEAS_COND_START_TEMPERATURE;
		iProfile.cmd_ctrl.Temp_ch = iProfile.temperature.MaxTempChannel;
		iProfile.cmd_ctrl.Temp_value = iProfile.temperature.MaxTemperature;
					
		if(iProfile.temperature.MaxTempChannel >= 0)
		{					
			if(iProfile.temperature.MaxTemperature > iProfile.meas_params_cur.Start_Params_1)
			{
				cond_finished = MEAS_COND_START_TEMPERATURE;
				return HMI_RET_NORMAL;
			}
		}	
	}
	else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TIME)
	{
		iProfile.cmd_ctrl.cond_stage = MEAS_COND_START_TIME;
		iProfile.cmd_ctrl.Temp_ch = iProfile.temperature.MaxTempChannel;
		iProfile.cmd_ctrl.Temp_value = iProfile.temperature.MaxTemperature;
		iProfile.cmd_ctrl.Time_value = iProfile.sys_status.CondTimer;
				
		if(cond_time_instatus==0)
		{
			cond_time_instatus = 1;
			iProfile.sys_status.CondStatus = 1;
			iProfile.sys_status.CondTimer = 0;	
		}	
		if(iProfile.sys_status.CondTimer > iProfile.meas_params_cur.Start_Params_2)
		{
			iProfile.sys_status.CondStatus = 0;
			iProfile.sys_status.CondTimer = 0;
			cond_finished = MEAS_COND_START_TIME;
			return HMI_RET_NORMAL;
		}
	}
	else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TorT)
	{
		
		iProfile.cmd_ctrl.cond_stage = MEAS_COND_START_TorT;
		iProfile.cmd_ctrl.Temp_ch = iProfile.temperature.MaxTempChannel;
		iProfile.cmd_ctrl.Temp_value = iProfile.temperature.MaxTemperature;
		iProfile.cmd_ctrl.Time_value = iProfile.sys_status.CondTimer;
		
		if(cond_time_instatus==0)
		{
			cond_time_instatus = 1;
			iProfile.sys_status.CondStatus = 1;
			iProfile.sys_status.CondTimer = 0;	
		}
						
		if(iProfile.temperature.MaxTempChannel!=0)
		{
			if(iProfile.temperature.MaxTemperature > iProfile.meas_params_cur.Start_Params_1)
			{
				cond_finished = MEAS_COND_START_TorT;
				return HMI_RET_NORMAL;
			}
		}
		
		if(iProfile.sys_status.CondTimer>iProfile.meas_params_cur.Start_Params_2)
		{
			iProfile.sys_status.CondStatus = 0;
			iProfile.sys_status.CondTimer = 0;
			cond_finished = MEAS_COND_START_TorT;
			return HMI_RET_NORMAL;
		}
			
	}
	else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TandT)
	{
		iProfile.cmd_ctrl.cond_stage = MEAS_COND_START_TandT;
		iProfile.cmd_ctrl.Temp_ch = iProfile.temperature.MaxTempChannel;
		iProfile.cmd_ctrl.Temp_value = iProfile.temperature.MaxTemperature;
		iProfile.cmd_ctrl.Time_value = iProfile.sys_status.CondTimer;
		
		if(cond_time_instatus==0)
		{
			cond_time_instatus = 1;
			iProfile.sys_status.CondStatus = 1;
			iProfile.sys_status.CondTimer = 0;	
			
			iProfile.cmd_ctrl.cond_stage = 0;
		}		
		
		if(cond_routine==0)
		{		
			if(iProfile.sys_status.CondTimer>iProfile.meas_params_cur.Start_Params_2)
			{
				iProfile.sys_status.CondStatus = 0;
				iProfile.sys_status.CondTimer = 0;
				
				iProfile.cmd_ctrl.cond_stage = 1;
				
				cond_routine = 1;
			}
		}
		else if(cond_routine==1)
		{				
			if(iProfile.temperature.MaxTemperature > iProfile.meas_params_cur.Start_Params_1)
			{
				cond_finished = MEAS_COND_START_TorT;
				return HMI_RET_NORMAL;
			}
		}
	}
	else if(iProfile.meas_params_cur.Start_Cond == MEAS_COND_START_TEtoTI)
	{		
		if(cond_time_instatus==0)
		{
            cond_routine = 0;
			cond_time_instatus = 1;
			iProfile.sys_status.CondStatus = 1;
			iProfile.sys_status.CondTimer = 0;	
		}
			
		if(iProfile.temperature.MaxTemperature > iProfile.meas_params_cur.Start_Params_1)
		{			
            cond_routine = 1;
				
			if(iProfile.sys_status.CondTimer>iProfile.meas_params_cur.Start_Params_2)
			{
				iProfile.sys_status.CondStatus = 0;
				iProfile.sys_status.CondTimer = 0;
				
				cond_finished = MEAS_COND_START_TEtoTI;
				return HMI_RET_NORMAL;
				
			}
		}
		else
		{
			cond_routine = 0;				
			iProfile.sys_status.CondTimer = 0;
		}
	}
	
#else
#error
#endif
		
    Cond_disp();
    
	iProfile.sys_status.CondTimer++;
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID)
{
	HMI_ENGINE_RESULT		eProcessResult;
	SGUI_UINT16				KeyValue;
	SGUI_UINT16				KeyAction;
	KEY_PRESS_EVENT			*pKeyEvent;
	SGUI_INT				ProcessAction;
	
	xSemaphoreTake(mtx_process_event, portMAX_DELAY);
	
	eProcessResult = HMI_RET_NORMAL;
	ProcessAction = HMI_DEMO_PROC_NO_ACT;
	
	if(EVENT_TYPE_ACTION == pEvent->iType)
	{
		if(EVENT_ID_KEY_PRESS == pEvent->iID)
		{			
			pKeyEvent = (KEY_PRESS_EVENT*)pEvent;
			KeyValue = EVENT_KEY_ID(pKeyEvent->Data.uiKeyValue);
			KeyAction = EVENT_KEY_ACTION(pKeyEvent->Data.uiKeyValue);	
			
			switch(KeyValue)
			{
				case KEY_PW:
				{				
											
#if DEV_TYPE == DEV_TYPE_I7		
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
						eProcessResult = HMI_RET_CANCEL_KEY;
						cond_finished = -1;	
                    }
                        
#elif DEV_TYPE == DEV_TYPE_XP
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
                        rses_led_control(&led_run,LED_COLOR_BLACK);
                        cond_finished = -1;	
                    }
                    
#elif DEV_TYPE == DEV_TYPE_MINI	
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
                        rses_led_control(&led_run,LED_COLOR_BLACK);
                        cond_finished = -1;	
                    }
#elif DEV_TYPE == DEV_TYPE_VACUUM
                    
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
                        rses_led_control(&led_run,LED_COLOR_BLACK);
                        cond_finished = -1;
                    }
                    
                    if(KeyAction == BUTTON_EVENT_DOUBLE)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
                        rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);
                        
                        cond_finished = MEAS_COND_START_MANUAL;
                    }
    
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
                    
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
                        oxy_sensor_stop();
                        
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
                        rses_led_control(&led_run,LED_COLOR_BLACK);
                        cond_finished = -1;	
                    }
                    
                    
#elif DEV_TYPE == DEV_TYPE_RPT_STD		

					if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
						cond_finished = -1;	
                        
                    }
                        
#else
#error
#endif		

				}break;
				
				case KEY_UP:
				{		
					if(KeyAction == BUTTON_EVENT_LONG)
					{	
						eProcessResult = HMI_RET_CANCEL_KEY;
#if DEV_TYPE == DEV_TYPE_I7							
						cond_finished = MEAS_COND_START_MANUAL;
#elif DEV_TYPE == DEV_TYPE_XP							
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);
                        
                        cond_finished = MEAS_COND_START_MANUAL;
                        
#elif DEV_TYPE == DEV_TYPE_MINI	
						
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);
                        
                        cond_finished = MEAS_COND_START_MANUAL;
                        
#elif DEV_TYPE == DEV_TYPE_VACUUM					
                        
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

                        rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);
                        
                        cond_finished = MEAS_COND_START_MANUAL;

#elif DEV_TYPE == DEV_TYPE_RPT_STD							
						cond_finished = MEAS_COND_START_MANUAL;		
#else
#error
#endif																			
					}
				}break;

                case KEY_DOWN:
				{		
					eProcessResult = HMI_RET_CANCEL_KEY;  
                }break;           
			}
		}
		else if(EVENT_ID_SCREEN == pEvent->iID)
		{
			EVENT_SRCEEN *temp_event;
			temp_event = (EVENT_SRCEEN*)pEvent;
			ProcessAction = HMI_DEMO_PROC_NO_ACT;
						
			if(temp_event->Data == 1)
			{
				UI_Refresh(pDev,NULL);
			}
		}
	}
	else if(EVENT_TYPE_DATA == pEvent->iType)
	{
	}
	
	if(pActionID != NULL)
	{
		*pActionID = ProcessAction;
	}
	
	xSemaphoreGive(mtx_process_event);
	
	return eProcessResult;
}

HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID)
{
	if(HMI_PROCESS_SUCCESSFUL(eProcResult))
	{
		
	}
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : cb_timer_autoback
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
static void cb_ui_timer(TimerHandle_t parameter)
{
	event_screen.Head.iType =	EVENT_TYPE_ACTION;
	event_screen.Head.iID 	=	EVENT_ID_SCREEN;
	event_screen.Data = 1;	
	HMI_ProcessEvent((HMI_EVENT_BASE*)&event_screen);
}
//

/********************************(C)COPYRIGHT 2016 RSEStudio ****************************/
/*****************************************END OF FILE************************************/

